import React from 'react';
import { inject, observer } from 'mobx-react';
import { Button, Spin,Modal, Row, Col, message, Tabs } from 'antd';
import {
  WeaTable, WeaBrowser, WeaLocaleProvider
} from 'ecCom';
const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;
@inject('planProgramViewStore')
@observer
export default class Audit extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedKeys: []
    };
    this.store = this.props.planProgramViewStore;
  }

  render() {
    const columns = [{
      title: getLabel(25034, '姓名'),
      // className: "column-text-center",
      dataIndex: 'name',
      key: 'name',
      width: "33%",
      sorter: false,
      render: (text, record, index) => {
        return this.getNameALink(record.id, record.name)
      }
    }, {
      title: getLabel(27511, '部门'),
      // className: "column-text-center",
      dataIndex: 'departmentname',
      key: 'departmentname',
      width: "33%",
      sorter: false
    }, {
      title: getLabel(33553, '分部'),
      // className: "column-text-center",
      dataIndex: 'subcompanyname',
      key: 'subcompanyname',
      width: "33%",
    }];
    const { auditList, buttonAuth, noDataMessage,auth } = this.store
    const { selectedKeys } = this.state
    let auditData = toJS(this.store.auditList)
    auditData = auditData.filter(a=>a.id!='null')
    return (
      <div className="report-audit">
        {(!buttonAuth.canSave && this.store.auditList.length < 1) ? <span class="comment-tipinfo">{getLabel(84304, "暂无")}</span> :
          <div>
            {
              auth.canedit &&  <div className='btn-area'>
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@vxcag4`} className='audit-btn' onClick={this.addManager2Audit}>{getLabel('15709', '直接上级')}</Button>
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@po5g62`} className='icon-btn' type="primary" onClick={() => { this.refs.audit.openModal() }}>{<i
                                        className="icon-coms-Add-to-hot"
                                    />}</Button>
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mrt9i7`} className='icon-btn' type="primary" onClick={this.delAudit}>{<i
                                        className="icon-coms-form-delete-hot"
                                        style={{color:`${selectedKeys.length>0?'#55a1f8':'#d8d8d8'}`}}
                                    />}</Button>
            </div>
            }
           {
             (auth.canedit || auth.canview) && <div>
             <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@0q35v6`} columns={columns} dataSource={auditData}
               pagination={false}
               rowSelection={{
                 selectedRowKeys: selectedKeys,
                 onChange: this.onSelectChange
               }}
             />
           </div>
           }
            
            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@28s653`} style={{ display: 'none' }}>
              <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@dnyrz9`} span={22}>
                <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@xck4iq`}
                  ref='audit'
                  replaceDatas={toJS(this.store.auditList)}
                  title={getLabel('505884', '报告审批')}
                  type={17}
                  viewAttr={buttonAuth.canSave ? '2' : '1'}
                  inputStyle={{}}
                  onChange={(ids, names, datas) =>
                    this.onAuditBrowerChange(ids, names, datas)}
                  isSingle={false}
                />
              </Col>
              <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@214kla`} span={2}>
                <div style={{ textAlign: 'center' }}>
                  {buttonAuth.canSave && this.store.resourcetype == 4 && <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ku6doy`} onClick={this.addManager2Audit}>{getLabel('15709', '直接上级')}</Button>}
                </div>
              </Col>
            </Row>
          </div>

        }
      </div>
    );
  }
  getNameALink = (resourceid, resourcename) => {
    let href = 'javaScript:openhrm(' + resourceid + ');';
    return (
      <a href={href} onClick={e => window.pointerXY(e)} class="autoWrap">
        {resourcename}
      </a>
    );
  };
  delAudit = () => {
    const { selectedKeys } = this.state
    const { auditids, auditList } = this.store
    if (selectedKeys.length > 0) {
      Modal.confirm({
        title: getLabel(386786, '信息确认'),
        content: getLabel(508867, '确定删除已选数据？'),
        onOk: () => {
          let auditIdList = auditids.split(",")
          let newList = auditIdList.map((v, i) => {
            if (selectedKeys.indexOf(i) < 0) {
              return v
            }
          })
          newList = newList.filter(n => n !== undefined)
          let newAuditList = auditList.filter(a => newList.indexOf(a.id) > -1)
          this.store.setState("auditList", newAuditList)
          this.store.setState("auditids", newList.join(","))
          this.setState({ selectedKeys: [] })
          this.store.saveTemplateConfig() 
        },
        onCancel: () => {
          return false;
        }
      })
    } else {
      message.error(getLabel(508946, '删除勾选不能为空'));
      return;
    }


  }
  onSelectChange = (selectedKeys, selectedRows) => {
    this.setState({ selectedKeys })
  }
  onAuditBrowerChange = (ids, names, datas) => {
    this.store.auditids = ids;
    this.store.auditList = datas
    this.store.saveTemplateConfig() 
  };
  //添加直接上级到列表
  addManager2Audit = () => {
    let { auditList, auditids, managerid, managermap } = this.store
    auditList = toJS(auditList)
    var flag = false
    if (managerid == '0') {
      message.info(getLabel('509340', '该用户没有直接上级！'));
      return
    } else {
      if (auditList && auditList.length > 0) {
        let hasManager = false
        auditList.map(v => {
          if (v.id == managerid) hasManager = true
        })
        if (!hasManager) {
          auditList.push(managermap)
          auditids += ("," + managerid + ",")
          flag=true
          //this.store.saveTemplateConfig() 
        } else {
          message.info(getLabel('509081', '上级领导已添加'));
        }
      } else {
        auditList = []
        auditList.push(managermap)
        auditids += managerid + ","
        flag=true
        //this.store.saveTemplateConfig() 
      }
    }
    this.store.auditList = auditList
    this.store.auditids = auditids
    if(flag)this.store.saveTemplateConfig()
  }
}
